<?php
/**
 * 短信任务
 */
class SmsTaskAction extends FrontAction{

    protected $type = array(
        0 => '混合',
        1 => '纯付费',
        2 => '纯普通玩家',
    );
    protected $status = array(
        0 => '未开始',
        1 => '发送中',
        2 => '发送完成',
    );

    public function __construct() {
        ini_set("memory_limit", "1024m");
        set_time_limit(60);
        parent::__construct();
    }

    public function doIndex(){
        $this->smarty->display('sms_task/index.html');
    }
    public function indexList(){
        $pageSize = AppCommon::getRequestInt("limit");
        $page = AppCommon::getRequestInt("page");
        $pageSize = $pageSize > 0 ? $pageSize : 10;
        $respage = $page > 0 ? $page : 1;
        $page = ($respage - 1) * $pageSize;

        $uri_key = AppCommon::getRequestString('uri_key');
        $starttime = AppCommon::getRequestString('starttime');

        $where = array();

        if ($uri_key){
            $where['key'] = $uri_key;
        }
        if ($starttime){
            $starttime = explode(' - ',$starttime);
            $where['insertymd'] = array('between',array(Common::toDate($starttime[0],'Ymd'),Common::toDate($starttime[1],'Ymd')));
        }

        $taskModule = new SmsTaskModule();
        $list = $taskModule->getSmsTaskList($where, '*', 'id desc', $page, $pageSize);
        $total = $taskModule->getSmsTaskList($where, 'count(1) as total');
        $total = $total ? intval($total[0]['total']) : 0;
        foreach ($list as &$val){
            $val['type'] = $this->type[$val['type']];
            $val['status'] = $this->status[$val['status']];
            $val['inserttime'] = Common::toDate($val['inserttime']);
            $val['updatetime'] =  Common::toDate($val['inserttime']);
//            $val['operate'] = '<a href="javascript:;" onclick="show_task_result(this);" data-id="'.$val['id'].'" class="layui-table-link" data-title="查看分析结果-'.$val['id'].'">查看分析结果</a>';
        }

        $result = array(
            'code' => 0,
            'count' => $total,
            'list' => $list,
        );

        $this->ajaxReturn($result);
    }

    public function doAdd(){
        if ($this->isPost()) {
            $title = AppCommon::getRequestString('title');
            $type = AppCommon::getRequestInt('type');
            $min_money_pay = AppCommon::getRequestInt('min_money_pay');
            $max_money_pay = AppCommon::getRequestInt('max_money_pay');
            $pay_num = AppCommon::getRequestInt('pay_num');
            $pay_num_zero = AppCommon::getRequestInt('pay_num_zero');

            if (empty($title)){
                $this->ajaxReturn(null, 400, '标题不能为空');
            }
            if ($type == 1 && empty($pay_num)){
                $this->ajaxReturn(null, 400, '需要付费数不能为空');
            }
            if ($type == 2 && empty($pay_num_zero)){
                $this->ajaxReturn(null, 400, '需要0元付费数不能为空');
            }
            if ($type == 0){
                if (empty($pay_num)){
                    $this->ajaxReturn(null, 400, '需要付费数不能为空');
                }
                if (empty($pay_num_zero)){
                    $this->ajaxReturn(null, 400, '需要0元付费数不能为空');
                }
            }

            $time = time();
            $data = array(
                'title' => $title,
                'type' => $type,
                'min_money_pay' => $min_money_pay,
                'max_money_pay' => $max_money_pay,
                'pay_num' => $pay_num,
                'pay_num_zero' => $pay_num_zero,
                'adminname' => $this->userinfo['account'],
                'inserttime' => $time,
                'insertymd' => date('Ymd'),
            );
            $taskModule = new SmsTaskModule();
            $insertId = $taskModule->add($data);
            if ($insertId === false) {
                $this->ajaxReturn(null, 400, '创建失败');
            }
            $this->ajaxReturn(null, 1, '操作成功');
        }

        $this->display("sms_task/add.html");
    }


}